package com.hsy.baoinbao.modules.main.ui.activity;

import android.os.Bundle;
import android.widget.ListView;

import com.android.volley.VolleyError;
import com.deparse.dpframe.utils.DPHttpUtil;
import com.deparse.dpframe.utils.DPLogUtil;
import com.hsy.baoinbao.R;
import com.hsy.baoinbao.common.Consts;
import com.hsy.baoinbao.modules.common.CommonBaseActivityWithSimpleActionBar;
import com.hsy.baoinbao.modules.main.ui.adapter.UserServiceListAdapter;
import com.hsy.baoinbao.modules.order.adapter.GoodsAllAdapter;
import com.hsy.baoinbao.utils.CommonUtils;
import com.hsy.baoinbao.utils.HttpUtil;
import com.jingchen.pulltorefresh.PullToRefreshLayout;
import com.jingchen.pulltorefresh.pullableview.PullableListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2016/5/25.
 */
public class UserServiceListActivity extends CommonBaseActivityWithSimpleActionBar {

    private ListView list;
    private UserServiceListAdapter userAdapter;
    private List<HashMap<String, String>> listData;
    private DPHttpUtil.HttpCallback httpCallBack = new DPHttpUtil.HttpCallback() {
        @Override
        public void onSuccess(int what, String response) {
            showUI(response);
            CommonUtils.hideProgressDialog();
        }

        @Override
        public void onError(int what, int type, VolleyError error) {
            CommonUtils.hideProgressDialog();
        }
    };

    private void showUI(String response) {

        try {
            JSONObject result = new JSONObject(response);
            JSONArray data = result.optJSONArray("data");
            if (data != null && data.length() > 0) {

                for (int i = 0; i < data.length(); i++) {
                    String id = data.optJSONObject(i).optString("id");
                    String number = data.optJSONObject(i).optString("number");
                    String name_merchant = data.optJSONObject(i).optString("name_merchant");

                    HashMap<String, String> map = new HashMap<>();
                    map.put("id", id);
                    map.put("number", number);
                    map.put("name_merchant", name_merchant);

                    listData.add(map);

                }
                userAdapter.notifyDataSetChanged();

            } else {

                DPLogUtil.d("data==null");
            }


        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_service);
        setTitle("服务商户");

        list = F(R.id.list);
        listData = new ArrayList<>();
        userAdapter = new UserServiceListAdapter(this, listData);
        list.setAdapter(userAdapter);


        CommonUtils.showProgressDialog(this);
        HttpUtil.httpGet(this, 0, Consts.API_GET_MER_LIST + "?token=" + CommonUtils.getToken(this), httpCallBack);
    }
}
